// pages/Device/Device.js
const httpRequest = require('../../utils/http_utils.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    PageSize: 10, //每页显示条数
    CurrentPage: 1, //当前页数
    ClassifyGuid: "", //设备类型唯一标识
    TaskType: 1, //任务类型
    maintainList: [], //维保设备集合
    TotalNum: 0, //设备数据数量
    showIs: false, //显示图片
    noMore: false, //显示提示语
    scrollHeight: 0, //自适应高度
    EquipmentName: "", //任务名称（输入框的值）
    Refresh:false, //是否刷新
    IsDelegate: 1, //是否外委单位
  },

  initData: function() {
    var that = this;
    var param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      ClassifyGuid: that.data.ClassifyGuid,
      EquipmentName: that.data.EquipmentName,
      TaskType: that.data.TaskType,
      IsDelegate: that.data.IsDelegate,
    }
    httpRequest.PostValidateRequest(httpRequest.apiInfo.GetMaintenanceTaskHisMobileApi, param,
      function(data) {
        console.log("-------------当天维保设备任务------------" + JSON.stringify(data))
        var listArray = that.data.maintainList;
        if (data.TotalNum == 0) {
          that.setData({
            showIs: true
          })
        } else {
          for (var i = 0; i < data.Data.length; i++) {
            listArray.push(data.Data[i])
          }
          that.setData({
            maintainList: listArray,
            TotalNum: data.TotalNum,
            noMore: false,
            showIs: false
          })
        }
      },
      function(err) {})
  },

  Device: function(e) {
    var equipmentname = e.currentTarget.dataset.equipmentname;
    var taskbegintime = e.currentTarget.dataset.taskbegintime;
    var taskendtime = e.currentTarget.dataset.taskendtime;
    var equipmentlocation = e.currentTarget.dataset.equipmentlocation;
    var taskguid = e.currentTarget.dataset.taskguid;
    wx.navigateTo({
      url: '../MaintainAdd/MaintainAdd?equipmentname=' + equipmentname + '&taskbegintime=' + taskbegintime + '&taskendtime=' + taskendtime + '&equipmentlocation=' + equipmentlocation + '&taskguid=' + taskguid
    })
  },

  //监听输入框
  searchInput: function(e) {
    this.setData({
      EquipmentName: e.detail.value,
      PageSize: 10,
      CurrentPage: 1,
      maintainList:[]
    })
    this.initData();
  },

  //上拉加载
  scrollToLower: function () {
    var that = this
    var pages = Math.round((that.data.TotalNum / 10) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.initData();
    }
  },

  /**
   * 
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      title: options.ClassifyName,
      ClassifyGuid: options.ClassifyGuid,
      scrollHeight: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 80
    })
    this.refreshView = this.selectComponent("#refreshView")
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      PageSize: 10,
      CurrentPage: 1,
      maintainList:[],
    })
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      this.setData({
        PageSize: 10,
        CurrentPage: 1,
        ClassifyGuid: this.data.ClassifyGuid,
        EquipmentName: this.data.EquipmentName,
        TaskType: 1,
        maintainList:[]
      })
      this.initData();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})